﻿using CRM.Infrastructure.EFCore.Mysql;
using Crm.Infrastructure.IOC;
using Crm.Service.Instance;
using CRM.Infrastructure.Attr;
using CRM.Infrastructure.Dto.Request.Login;
using CRM.Infrastructure.Dto.Response;
using CRM.Infrastructure.EFCore.Mysql;
using CRM.Infrastructure.Tools;
using CRM.Service.EFCore;
using CRM.Service.Interface;
using Microsoft.EntityFrameworkCore;

namespace CRM.Service.Instance
{
    [Inject_]
    public class Login_Service : Base_Service, I_Login_Service
    {
        private readonly Login_IOC _login_IOC;

        public Login_Service(Login_IOC login_IOC)
        {
            _login_IOC = login_IOC;


        }

        /// <summary>
        /// 根据最后登录的code 获取登录用户
        /// </summary>
        /// <param name="code"></param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public async Task<SysUser?> Get_User_By_LastCode(string code, string ip)
        {
            var iq = _login_IOC._sys_Login_Log_EFCore.QueryAll(d => d.LlCode == code
            && d.LlIp == ip);
            if (!await iq.AnyAsync())
            {
                return null;
            }
            var log = await iq.SingleAsync();
            var last = await _login_IOC._sys_Login_Log_EFCore.QueryAll(false, o => o.LlCreateTime, d => d.LlUid == log.LlUid).FirstAsync();

            if (log.LlId != last.LlId)
            {
                return null;
            }
            return await _login_IOC._sys_User_EFCore.QueryAll(d => d.UId == log.LlUid).SingleAsync();


        }
        /// <summary>
        /// 管理员登陆
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<Api_Response_Dto> Logins(Login_Request_Dto dto)
        {
            var iq = _login_IOC._sys_User_EFCore.QueryAll(d => d.UAccount == dto.account);

            if (!await iq.AnyAsync())
            {
                return Result(0, "账号或者密码错误");
            }
            var user = await iq.SingleAsync();//如果你的结果集里面没有数据则会报错
            if (user.UIsActive == 0)
            {
                return Result(0, "账号未激活", new
                {
                    active = 0
                });
            }
            if (user.UIsBan == 1)
            {
                return Result(0, "账号已禁用", new
                {
                    ban = 1
                });
            }
            var pwd = EncryptUtil.LoginMd5(dto.password!, user.USalt!);
            if (user.UPassword != pwd)
            {
                return Result(0, "账号或者密码错误");
            }

            //将code写入登录日志。

            SysLoginLog sysLoginLog = new SysLoginLog()
            {
                LlId = Config.GUID_1(),
                LlCode = dto.code,
                LlCreateTime = DateTime.Now,
                LlExplain = null,
                LlIp = Config.GetIp(),
                LlResult = 0,
                LlType = "admin",
                LlUid = user.UId,
            };

            _login_IOC._sys_Login_Log_EFCore.Add(sysLoginLog);
            _login_IOC._sys_Login_Log_EFCore.SaveChanges();
            return Result(1, "ok");
        }

    }
}
